﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using NASA.BeAMartian.Plugin;
using NASA.BeAMartian.Helper;

namespace NASA.BeAMartian.Execution
{
    public class XapLoaderExecutionItem : ExecutionItem
    {
        public XapLoaderExecutionItem(
            XapClass xapClass,
            object[] contructorArgs,
            object userToken,
            EventHandler<XapDownloadCompletedEventArgs> xapDownloadCompletedEvent,
            EventHandler<XapProgressEventArgs> xapDownloadProgressChangedEvent)
        {
            this.XapClass = xapClass;
            this.ContructorArgs = contructorArgs;
            this.UserToken = userToken;
            this.XapDownloadCompletedEvent = xapDownloadCompletedEvent;
            this.XapDownloadProgressChangedEvent = xapDownloadProgressChangedEvent;
        }

        public XapLoader XapLoader { get; set; }
        public XapClass XapClass { get; set; }
        public object[] ContructorArgs { get; set; }
        public object UserToken { get; set; }
        public EventHandler<XapDownloadCompletedEventArgs> XapDownloadCompletedEvent { get; set; }
        public EventHandler<XapProgressEventArgs> XapDownloadProgressChangedEvent { get; set; }

        public override void Invoke()
        {
            XapLoaderHelper.Instance.XapLoader.DownloadCompleted += new EventHandler<XapDownloadCompletedEventArgs>(XapLoader_DownloadCompleted);
            XapLoaderHelper.Instance.XapLoader.DownloadProgressChanged += new EventHandler<XapProgressEventArgs>(XapLoader_DownloadProgressChanged);
            XapLoaderHelper.Instance.XapLoader.LoadXapAsync(this.XapClass.XapUri, new XapLoaderUserToken(this.XapClass.ClassName, this.ContructorArgs, this.UserToken));
        }

        private void XapLoader_DownloadProgressChanged(object sender, XapProgressEventArgs e)
        {
            this.XapDownloadProgressChangedEvent(sender, e);
        }

        private void XapLoader_DownloadCompleted(object sender, XapDownloadCompletedEventArgs e)
        {
            if (this.IsCanceled)
            {
                XapDownloadCompletedEventArgs args = new XapDownloadCompletedEventArgs(e.XapUri, e.UserToken, true, e.Exception);
                this.XapDownloadCompletedEvent(sender, e);
            }
            else
            {
                this.XapDownloadCompletedEvent(sender, e);
            }

            XapLoaderHelper.Instance.XapLoader.DownloadCompleted -= new EventHandler<XapDownloadCompletedEventArgs>(XapLoader_DownloadCompleted);
            XapLoaderHelper.Instance.XapLoader.DownloadProgressChanged -= new EventHandler<XapProgressEventArgs>(XapLoader_DownloadProgressChanged);
            this.OnActionCompleted(e.Exception, this.IsCanceled);
        }
    }
}
